package com.luoke.handler.request;

import cn.hutool.json.JSONUtil;
import com.luoke.connect.GameWebSocketClient;
import com.luoke.constants.ModuleTypeConstant;
import com.luoke.constants.SubmoduleTypeConstant;
import com.luoke.context.UserContext;
import com.luoke.pojo.dto.BattleRoundDTO;
import com.luoke.result.Result;
import com.luoke.service.request.BattleRequestService;
import com.luoke.service.request.UserRequestService;
import com.luoke.service.request.impl.BattleRequestServiceImpl;
import com.luoke.service.request.impl.UserRequestServiceImpl;

import java.util.Scanner;

/**
 * 战斗请求处理层，负责请求的分发
 */
public class BattleRequestHandler {

    private BattleRequestService battleRequestService = new BattleRequestServiceImpl();
    private UserRequestService userRequestService = new UserRequestServiceImpl();


    /**
     * 游戏菜单
     * @param gameWebSocketClient 使用该对象与服务端通信
     */
    public void showGameMenu(GameWebSocketClient gameWebSocketClient){

        int choose;
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("\n===== 洛克宠物游戏菜单( "+ UserContext.getCurrentUser() +" ) =====");
            System.out.println("1. 玩家对战 (PvP)");
            System.out.println("2. 探索");
            System.out.println("3. 查看背包");
            System.out.println("4. 查看宠物");
            System.out.println("5. 查看在线玩家");
            System.out.println("6. 退出登录");
            System.out.print("请输入您的选择: ");
            choose = scanner.nextInt();
            scanner.nextLine();//删除换行符
            if (choose >= 1 && choose <= 6) {
                break;
            }
            System.out.println("无效选择，请重新输入。");
        }
        switch (choose) {
            case 1:
                showPvpMenu(gameWebSocketClient,scanner);//进入二级菜单
                break;
            case 2:
                showShop(gameWebSocketClient);
                break;
            case 3:
                showBagMenu(gameWebSocketClient);
                break;
            case 4:
                showPetMenu(gameWebSocketClient);
                break;
            case 5:
                showOnlineUser(gameWebSocketClient);
                break;
            case 6:
                userRequestService.logout(gameWebSocketClient,scanner);
                break;
            default:
                System.out.println("无效选择，请重新输入。");
                break;
        }
    }

    private void showOnlineUser(GameWebSocketClient gameWebSocketClient) {
        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.USER);
        request.setCommand(SubmoduleTypeConstant.SHOW_USER_ONLINE_NAME);
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    public void showShop(GameWebSocketClient gameWebSocketClient) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("-----------探索----------");
        System.out.println("1.赚钱小游戏");
        System.out.println("2.商店");
        System.out.println("3.探索地图");
        System.out.println("4.返回");
        System.out.println("-----------探索----------");
        String line = scanner.nextLine();
        if (line.equals("4")) {
            showGameMenu(gameWebSocketClient);
            return;
        }
        if ("1".equals(line)) {
            showMiniGame(gameWebSocketClient,scanner);
            return;
        }
        if ("2".equals(line)) {
            userRequestService.getShop(gameWebSocketClient);
            return;
        }
        if ("3".equals(line)) {
            userRequestService.getMap(gameWebSocketClient);
            return;
        }
    }
    public void showPetMenu(GameWebSocketClient gameWebSocketClient) {
        userRequestService.getPets(gameWebSocketClient);
    }

    public void showBagMenu(GameWebSocketClient gameWebSocketClient) {
        userRequestService.getBag(gameWebSocketClient);
    }

    /**
     * 展示玩家对战的二级菜单
     * @param gameWebSocketClient 使用该对象与服务端通信
     * @param scanner 输入流
     */
    public void showPvpMenu(GameWebSocketClient gameWebSocketClient,Scanner scanner){
        int choose;
        while (true){
            System.out.println("\n===== 洛克宠物游戏菜单( "+UserContext.getCurrentUser()+" ) =====");
            System.out.println("1. 发起对战");
            System.out.println("2. 等待对战"); //想进行邀请对战首先必须有一方进入的等待对战的状态，由另外一方进行发起对战
            System.out.println("3. 匹配对战"); //匹配对战匹配的是在等待区的人，如果没人则一直等待，或者超时自动返回菜单，匹配到后自动发起请求，待对方同意之后进行对战
            System.out.println("4. 返回菜单");
            System.out.print("请输入您的选择: ");
            choose = scanner.nextInt();
            scanner.nextLine();//删除换行符
            if (choose >= 1 && choose <= 4) {
                break;
            }
            System.out.println("无效选择，请重新输入。");
        }
        switch (choose) {
            case 1:
                battleRequestService.startPvpBattle(gameWebSocketClient, scanner);
                break;
            case 2:
                battleRequestService.userToWait(gameWebSocketClient, scanner); //当前用户进入等待区的时候，要给服务器发送请求，把该用户加入等待区
                break;
            case 3:
                battleRequestService.matchBattle(gameWebSocketClient, scanner);
                break;
            case 4:
                showGameMenu(gameWebSocketClient);
                break;
            default:
                System.out.println("无效选择，请重新输入。");
                break;
        }
    }

    public void showMiniGame(GameWebSocketClient gameWebSocketClient,Scanner scanner){
        String number = "";
        while (true){
            System.out.println("--------赚钱小游戏---------");
            System.out.println("1.吃苹果小游戏");
            System.out.println("2.走迷宫小游戏");
            System.out.println("3.二十四点小游戏");
            System.out.println("4.返回上一级");
            System.out.println("--------赚钱小游戏---------");
            System.out.println();
            System.out.println("请输入你要进行的游戏序号: ");
            number = scanner.nextLine();
            if (number.equals("1")||number.equals("2")||number.equals("3")||number.equals("4")) {
                break;
            }
            System.out.println("无效选择，请重新输入。");
        }
        switch (number) {
            case "1":
                userRequestService.eatAppleGame(gameWebSocketClient, scanner);
                showShop(gameWebSocketClient);
                break;
            case "2":
                userRequestService.MazeGame(gameWebSocketClient, scanner);
                showShop(gameWebSocketClient);
                break;
            case "3":
                userRequestService.TwentyFourGame(gameWebSocketClient, scanner);
                showShop(gameWebSocketClient);
                break;
            case "4":
                showShop(gameWebSocketClient);
                break;
        }



    }


    /**
     * 展示战斗背包菜单的请求
     * @param gameWebSocketClient
     */
    public void showBattleBagMenu(GameWebSocketClient gameWebSocketClient) {
        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.BATTLE_ROUND);
        request.setCommand(SubmoduleTypeConstant.BAG);
        request.setData(UserContext.getCurrentUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    /**
     * 展示战斗宠物菜单
     * @param gameWebSocketClient
     */
    public void showBattlePetMenu(GameWebSocketClient gameWebSocketClient) {
        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.BATTLE_ROUND);
        request.setCommand(SubmoduleTypeConstant.PETS);
        request.setFromUser(UserContext.getCurrentUser()); //获取到当前的用户
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }

    /**
     * 展示战斗宠物技能的菜单的请求
     * @param gameWebSocketClient
     */
    public void showBattleSkillMenu(GameWebSocketClient gameWebSocketClient, BattleRoundDTO data) {

        Result<String> request = new Result<>();
        request.setModuleName(ModuleTypeConstant.BATTLE_ROUND);
        request.setCommand(SubmoduleTypeConstant.SKILLS);
        request.setFromUser(UserContext.getCurrentUser()); //获取到当前的用户
        request.setData(UserContext.getCurrentPet().getPetName()); //传当前出战宠物的目的是为了查找当前宠物所对应的技能
        gameWebSocketClient.send(JSONUtil.toJsonStr(request));
    }
}
